

   class ListNode {
      int val;
      ListNode next;
      ListNode() {}
     ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head==null) return null;
        int n=k;
        ListNode cur=head;

        while(n>0){
            if(cur==null) return head;
            n--;
            cur=cur.next;
        }
        ListNode phead=new ListNode();
        cur=head;

        ListNode next=cur.next;
        n=k;

        while(n>0){
            next=cur.next;
            cur.next=phead.next;
            phead.next=cur;
            cur=next;
            n--;
        }

        head.next= reverseKGroup(next,k);
        return phead.next;
    }
}